<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Forum/classes/PhpFox_ComponentPostReply.class.php, $Author:  $ */



/** Post reply component
 *
 * 1. oCurrUser (optional) current user object; if null or omitted - used current user
 *
 * @package module.forum
 * @version $Id: PhpFox_ComponentPostReply.class.php 297 2008-02-07 15:05:58Z phpfoxsv $
 *
 */
class PhpFox_Mod_Forum_ComponentPostReply extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        $oSrvSec->checkPerm('forum', 'full');

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    $oTxt = App::newObject('Text');
	    
	    $oUser = $this->getParam('oUser', null);
	    if (!$oUser)
	    {
	        $oSrvSec = &App::getModuleService('Account', 'Security');
	        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
	        $oUser = $oSrvSec->getCurrentUser();
	    }

        //get quoted text if any
        $oTopic = $this->_oModule->getItem('ForumTopic');
        
        /*$aValFields = array(
	        'text'  => array('title'=> App::format('Mod_Forum.forum_message'), 'def'=>'required'),
	    );

	    $oValidator = App::newObject('Validator', $aValFields);
	    */

        $sText = '';
        $sQuotedUser = '';
        if ('start' == $oReq->get('quote'))
        {
            $oTopic->load($oReq->getInt('id'));
            $sQuotedUser = $oTopic->aData['user'];
            $sText = '[quote="'.$oTopic->aData['user'].'"]'.$oTopic->aData['text'].'[/quote]';
        }
        elseif($oReq->getInt('quote'))
        {
            $oPost = &$this->_oModule->getItem('ForumReply');
            $oPost->load($oReq->getInt('quote'));
            $sQuotedUser = $oPost->aData['user'];
            $sText = '[quote="'.$oPost->aData['user'].'"]' . $oPost->aData['text'] . '[/quote]';
         }
        elseif($oReq->getInt('postedit'))
        {
            $oPost = &$this->_oModule->getItem('ForumReply');
            $oPost->load($oReq->getInt('quote'));
            $sText = $oPost->aData['text'];
        }

        $oTopic = $this->_oModule->getItem('ForumTopic');
        switch($oReq->getAction())
        {
            case 'forum_post_reply':
                if ($oUser)
                {
                    $oSrvForum = &$this->_oModule->getService('Forum');
                    /* @var $oSrvForum PhpFox_Mod_Forum_ServiceForum */

                    if ($oTopic->load($oReq->getInt('id')))
                    {
                    	 $sText = $oReq->get('text');
                    	 $sText = $oTxt->parseText($oReq->get('text'),0,0,1);

						 if ( '' != trim(strip_tags($sText)) )
						 {
                         	if ($iReply = $oSrvForum->addReply($oUser, $oTopic, $oTxt->parseText($oReq->get('text'),0,0,1), $sQuotedUser))
                        	{
                                $oSrvAcc = &App::getModuleService('Account', 'Account');
                                $oSrvAcc->changeUserInfo($oUser, 'forum');
                        	    $aParams = array(
                        	        'page' => 'last', //realy big  to get last page
                        	        'id'   => $oReq->getInt('id'),
                        	        //'#'    => 'r'.$iReply,  -- problem in IE
                        	    );
                        	    App::gotoUrl('public.forum.posts', $aParams);
                        	}
                         }else{
                         	$this->addErrors(App::format('validator.rule.req',	array(App::format('message'), App::format('message')) ));
                         }
                    }
                }
                break;
        }

		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
            'sText' => $sText,
            'sSubmit' => App::format('install.submit'),
            'aErrors'     => $this->getErrors(),
        ));
		$srcHtml = $oTpl->fetch('PostReply.html');
		return $srcHtml;
	}

}
?>